define(['../libs/zepto',
	//	'../common/base',
	'../common/ajax',
	'../common/yingSoftPrg',
	'../common/yingSoftCookie',
	'../common/storage',
	'../common/dialog',
	'../find/defaultJSON'
], function() {
	var isvideo = 0;
	this.init = function() {

		setCss();
		_details();
		Click(); //判断充值码是否正确
		clickBtn();
		iOSvideopay();

	}

	function setCss() {
		//		var height = GetsingleHeight();

		$('.rechargeBox').css('height', '200px');

	}

	function getVipEndTime() {
		var jsonData = '';
		var getUserID = G_Cookie.getUserID();
		var getGuid = G_Cookie.getGuid();

		//判断使用者的id和通行证正确与否，不是的话返回掉
		if(!getUserID || !getGuid) {
			return;
		}
		//获取科目ID属性
		var appID = G_Cookie.getAppID();

		var url = "/api/user/getVipApp/" + getUserID + "/" + getGuid;

		G_AjaxApi_Record.get(url, true, function(json) {
			var jsonData = JSON.parse(json);
			if(jsonData.status === 200) {
				var allData = jsonData.data.userAppJson.VipApp;
				for(var i = 0; i < allData.length; i++) {
					if(appID == jsonData.data.userAppJson.VipApp[i].AppID) {
						var endTime = jsonData.data.userAppJson.VipApp[i].EndTime;
						var ed = G_Prg.datetimeFormat(new Date(endTime), 'yyyy-MM-dd hh:mm:ss');
						$('.recharge_ullist li:last-child').css('display', 'block');
						$('.arrive_data').html(ed);
						$('.overdue').hide();
						break;
					} else {
						$(".recharge_ullist li:last-child").hide();
						$('.overdue').show();
					}
				}

			} else if(jsonData.status === 201) {
				$('.overdue').show();
				$("ul li:last-child").hide();
			}

		});

	};
	//浏览器类型
	function _getClientType() {
		var browser = {
			versions: function() {
					var u = navigator.userAgent,
						app = navigator.appVersion;
					return {
						ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
						android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
						iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
						iPad: u.indexOf('iPad') > -1, //是否iPad
						webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
					};
				}
				()
		};
		if(browser.versions.iPhone || browser.versions.iPad) {
			return 'web_iPhone';
		}
		if(browser.versions.ios || browser.versions.android || browser.versions.webApp) {
			return 'web_android';
		}
		return 'web_pc';
	}

	function _details(endTime) {
		// var cur = '医学高级';
		var _appEName = G_Cookie.getAppEName();

		var json = {
			tr1: G_Cookie.getUserName(),
			tr2: G_Cookie.getAppName()
		}
		if(json.tr2.indexOf('］')) {
			var _title = json.tr2;
			var p = /\［.*?\］/g;
			json.tr2 = _title.replace(p, '');
		}
		var html = '<li>通行证号：<span class="zhanghao">' + json.tr1 + '</span></li><li>充值科目：<span class="kemu">' + json.tr2 + '</span><li>到期时间：<span class="arrive_data"></span></li></li> '
		getVipEndTime();
		$('.recharge_ullist').append(html);
		var html2 = '<span class="overdue">未充值</span>';
		$('.contentBox').append(html2);
		var str = ' <div class="rechargeBox"></div>';
		$('.rich').append(str);
		//      
		$('.rechargeBox').css('display', 'block');
		if(_getClientType() == "web_android") {
			if(_appEName in defaultJson.videoAppEnameArr) {
				//显示视频课程按钮
				$('.rechargeBox').css('display', 'block');
				$('.codeBOX').css('display', 'block');
				$('.onlinePay').css('display', 'block');
				$('.onlineVideo').css('display', 'block');
			} else {
				$('.rechargeBox').css('display', 'block');
				$('.codeBOX').css('display', 'block');
				$('.onlinePay').css('display', 'block');
				$('.onlineVideo').css('display', 'none');
			}

		} else if(_getClientType() == "web_iphone" || "ipad" || "iphone") {
			if(_appEName in defaultJson.videoAppEnameArr) {
				isvideo = 1;
				$('.rechargeBox').css('display', 'block');
				$('.codeBOX').css('display', 'none');
				$('.onlinePay').css('display', 'block');
				$('.onlineVideo').css('display', 'block');
			} else {
				isvideo = 0;
				$('.rechargeBox').css('display', 'block');
				$('.codeBOX').css('display', 'none');
				$('.onlinePay').css('display', 'block');
				$('.onlineVideo').css('display', 'none');
			}
		}

		$('.contentBox').css('background-color', '#f0f0f0');
	};
	//===============

	function iOSvideopay() {

	}

	function Click() {
		$(document).on('click', '.codeBtn', function() {
			var getUserID = G_Cookie.getUserID();
			var getAppEName = G_Cookie.getAppEName();
			var regCode = $('.text').val();
			regCode = regCode.toString().trim();
			regCode = regCode.replace(/[\ ]/g, '');

			if(!getUserID) {
				return;
			}
			if(!regCode || regCode.toString().trim() === '') {
				G_Prg.alert('充值码不能为空');
				return;
			} else if(regCode && regCode.length !== 20) {
				G_Prg.alert('请输入20位长度的充值码');
				return;
			}
			if(!getUserID || getUserID.toString().trim() === '') {
				G_Prg.alert('用户Cookie信息获取失败');
				return;
			}
			if(!getAppEName || getAppEName.toString().trim() === '') {
				G_Prg.alert('软件Cookie信息获取失败，');
				return;
			}

			var appID = G_Cookie.getAppID();
			var url = "/api/price/userOpenApp/" + regCode + "/" + getUserID + '/' + getAppEName;

			var success = function(json) {
				var jsonData = JSON.parse(json);
				if(jsonData.status === 200) {
					G_Prg.alert('充值成功',function(){
						window.location.href = '/doExam-index';
					});
					return;
				}
				G_Prg.alert(jsonData.msg);

			};
			G_AjaxApi_Record.get(url, true, success);
			if(regCode === '01234567890123456789') {
				G_AjaxApi_Record.post('/api/limitedCode/useCode', {
					code: regCode,
					userName: G_Cookie.getUserName(),
					appID: G_Cookie.getAppID(),
					device: G_Prg.getCookie('PHONEID')
				}, true, function(json) { //改传userID
					var jsonData = JSON.parse(json);
					if(jsonData && (jsonData.status === 200)) {
						window.location.href = 'doExam-index?status=200&appID=' + jsonData.data;
						return;
					}
					if(jsonData && (jsonData.status === 201)) {
						G_Prg.alert('充值太过频繁，请休息60秒后再试');
						return;
					}
					if(jsonData && jsonData.status === 400) {
						G_Prg.alert(jsonData.msg);
						return;
					}
					if(jsonData && jsonData.status > 400 && jsonData.status < 500) {
						G_Prg.alert(jsonData.msg);
						return;
					}
					G_Prg.throw('程序运行错误:userHome.userHome.activate  数据异常：' + JSON.stringify(jsonData));
				});
			} else {
				G_AjaxApi_Record.get('/api/price/userOpenApp/' + regCode + '/' + userID + '/' + appEName, true, function(json) {
					var jsonData = JSON.parse(json);
					if(jsonData && (jsonData.status === 200)) {
						G_Prg.alert('充值成功',function(){
							window.location.href = '/doExam-index';
						});
						return;
					}
					if(jsonData && (jsonData.status === 201)) {
						G_Prg.alert(jsonData.msg);
						return;
					}
					if(jsonData && jsonData.status === 400) {
						G_Prg.alert(jsonData.msg);
						return;
					}
					G_Prg.throw('程序运行错误:userHome.userHome.activate  数据异常：' + JSON.stringify(jsonData));
				});
			}
		})
	}

	function clickBtn() {
		$('.back').on('click', function() {
			var fromUrl = G_Prg.getQueryString('fromUrl');
			if(fromUrl == 'videoList') {
				window.location.href = '/videoList';
			} else if(fromUrl == 'yhyc') {
				window.location.href = '/yhyc';
			} else if(fromUrl == 'knowledgeChapter') {
				window.location.href = '/knowledgeChapter';
			} else if(fromUrl == 'knowledgeList') {
				window.location.href = '/knowledgeChapter';
			}

		})
		$(".btnDialog").on("click", function() {
				return;
			})
			//在线题库充值========================================
		$('.onlinePay').on('click',function() {
			//浏览器类型
			//			function _getClientType() {
			//				if(navigator.userAgent.indexOf("MicroMessenger") !== -1) {
			//					return 'web_messenger';
			//				} else if(navigator.userAgent.indexOf("Windows Phone") !== -1) {
			//					return 'web_winphone';
			//				} else if(navigator.userAgent.indexOf("iPhone") !== -1) {
			//					return 'web_iphone';
			//				} else if(navigator.userAgent.indexOf("Android") !== -1) {
			//					return 'web_android';
			//				} else {
			//					return 'web_pc';
			//				}
			//			}
			if(_getClientType() == "web_android") {
				window.location.href = '/recharge/buyApp';

			} else if(_getClientType() == "web_iphone" || "ipad" || "iphone") {
				var userID_str = G_Cookie.getUserID();
				var UserName_str = G_Cookie.getUserName(); //变量存储用户ID;—>

				var AppEName_str = G_Cookie.getAppEName(); //变量存储当前将要购买的科目名称;—>

				var AppName_str = encodeURI(G_Cookie.getAppName()); // 字符串转换utf-8

				var ClassInfo_json = JSON.stringify({
					"UserName": UserName_str,
					"userID": userID_str,
					"AppEName": AppEName_str,
					"AppName": AppName_str
				});
				console.log(UserName_str + "@@" + userID_str + "@@" + AppName_str + "@@" + AppEName_str + "@@" + ClassInfo_json)
				parent.window.location.href = "iospay://" + "@@" + UserName_str + "@@" + userID_str + "@@" + AppName_str + "@@" + AppEName_str + "@@" + ClassInfo_json;

			}
		});
		$('.onlineVideo').on('click',function() {
			if(_getClientType() == "web_android") {
				console.log(123)
				window.location.href = '/onlineVideo?fromUrl=recharge';

			} else if(_getClientType() == "web_iphone" || "ipad" || "iphone") {
				console.log(456)
				console.log(isvideo)
				var userID_str = G_Cookie.getUserID(); //变量存储用户ID;—>

				var UserName_str = G_Cookie.getUserName(); //变量存储用户ID;—>

				var AppEName_str = G_Cookie.getAppEName(); //变量存储当前将要购买的科目名称;—>

				var AppName_str = encodeURI(G_Cookie.getAppName()); // 字符串转换utf-8 

				parent.window.location.href = "iosvideopay://" + UserName_str + "@@" + userID_str + "@@" + AppName_str + "@@" + AppEName_str + "@@" + isvideo;

			}
		})
	}
	init();
});